Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Kólko i krzyżyk


Rekomendowane odpowiedzi

Opublikowano

Znana i prosta gra ;)

 

 

 

#include <array.au3>
opt('guioneventmode',1)
$komputer = False
$1 = 9
dim $tab[3][3] = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Kółko i krzyżyk", 200, 238, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
dim $Button[3][3]
$wygrane = 0
$przegrane = 0
$remis = 0
for $x=0 to 2
	for $y = 0 to 2
$Button[$x][$y] = GUICtrlCreateButton(" ", 40+$x*36, 40+$y*26, 35, 25, BitOR($BS_CENTER,$BS_VCENTER))
GUICtrlSetOnEvent(-1,'user')
GUICtrlSetFont(-1, 17, 800, 0, "MS Sans Serif")
	Next
Next

$w =GUICtrlCreateLabel('Wygrane: '&$wygrane,10,150)
$p =GUICtrlCreateLabel('Przegrane: '&$przegrane,10,170)
$r =GUICtrlCreateLabel('Remis: '&$remis,10,190)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
	if $komputer Then
		ruch_komputera()
	EndIf

sleep(200)
WEnd


func user()
zaznacz(@GUI_CtrlId)
EndFunc

func zaznacz($przycisk)
$znak = 'X'
$klik = False
	if $przycisk = 3 Then
		if $tab[0][0] = ' ' Then
			$tab[0][0] = $znak
			$klik = True
		EndIf
	ElseIf $przycisk = 4 Then
		if $tab[1][0] = ' ' Then
			$tab[1][0] = $znak
			$klik = True
		EndIf
	ElseIf $przycisk = 5 Then
		if $tab[2][0] = ' ' Then
		$tab[2][0] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 6 Then
		if $tab[0][1] = ' ' Then
		$tab[0][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 7 Then
		if $tab[1][1] = ' ' Then
		$tab[1][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 8 Then
		if $tab[2][1] = ' ' Then
		$tab[2][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 9 Then
		if $tab[0][2] = ' ' Then
		$tab[0][2] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 10 Then
		if $tab[1][2] = ' ' Then
		$tab[1][2] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 11 Then
		if $tab[2][2] = ' ' Then
		$tab[2][2] = $znak
		$klik = True
		EndIf

	EndIf

if $klik Then
	rysuj()
$komputer = True
EndIf

EndFunc

func rysuj()
for $x=0 to 2
		for $y = 0 to 2
		GUICtrlSetData($Button[$y][$x],$tab[$x][$y])

		Next
	Next

sprawdz()
EndFunc




func ruch_komputera()
	;blokuj
	;wygraj
	;random

$w = wygraj()
if $w = 0 Then
	$b = blokuj()
	if $b = 0 Then
		losowo()
	EndIf
EndIf

rysuj()

$komputer = False

EndFunc


func sprawdz()
$1-=1
if bitor(BitAND($tab[0][0]=$tab[0][1],$tab[0][1] = $tab[0][2],$tab[0][2] <>' '), _
	BitAND($tab[1][0]=$tab[1][1],$tab[1][1] = $tab[1][2],$tab[1][2] <>' '), _
	BitAND($tab[2][0]=$tab[2][1],$tab[2][1] = $tab[2][2],$tab[2][2] <>' '), _
	BitAND($tab[0][0]=$tab[1][0],$tab[1][0] = $tab[2][0],$tab[2][0] <>' '), _
	BitAND($tab[0][1]=$tab[1][1],$tab[1][1] = $tab[2][1],$tab[2][1] <>' '), _
	BitAND($tab[0][2]=$tab[1][2],$tab[1][2] = $tab[2][2],$tab[2][2] <>' '), _
	BitAND($tab[0][0]=$tab[1][1],$tab[1][1] = $tab[2][2],$tab[2][2] <>' '), _
	BitAND($tab[2][0]=$tab[1][1],$tab[1][1] = $tab[0][2],$tab[0][2] <>' '))Then

if $komputer Then
	MsgBox('','','Komputrer wygrał')
	$przegrane+=1
	GUICtrlSetData($p,'Przegrane: '&$przegrane)
Else
	MsgBox('','','Wygrałeś')
	$wygrane+=1
	GUICtrlSetData($w,'Wygrane: '&$wygrane)
EndIf

reset()
Else
	if $1 = 0 then
		MsgBox('','','Remis')
		$remis+=1
		GUICtrlSetData($r,'Remis: '&$remis)

		reset()
		Else
	ConsoleWrite(' '&@CRLF)
EndIf

EndIf
EndFunc

func reset()
	$komputer = False
$1 = 9
dim $tab[3][3] = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']]
for $x=0 to 2
		for $y = 0 to 2
		GUICtrlSetData($Button[$y][$x],$tab[$x][$y])

		Next
	Next
EndFunc


func losowo()
	$koniec = False
	if $tab[1][1] = ' ' then
	$tab[1][1] = 'O'
	Else
do
$x = Random(0,2,1)
$y  =Random(0,2,1)
if $tab[$x][$y] = ' ' Then
	$tab[$x][$y] = 'O'
	$koniec = True
EndIf
until $koniec
EndIf


EndFunc

func wygraj()
ConsoleWrite('próba  wygrania'&@CRLF)
$w = False
for $x = 0 to 2
	if ($tab[$x][0] = 'O' and $tab[$x][1] = 'O' and $tab[$x][2] = ' ' ) Then
		$tab[$x][2] = 'O'
		$w = True
	ElseIf ($tab[$x][1] = 'O' and $tab[$x][2] = 'O' and $tab[$x][0] = ' ') Then
		$tab[$x][0] = 'O'
		$w = True
	ElseIf ($tab[$x][0] = 'O' and $tab[$x][2] = 'O' and $tab[$x][1] = ' ') Then
		$tab[$x][1] = 'O'
		$w = True
	ElseIf ($tab[0][$x] = 'O' and $tab[1][$x] = 'O' and $tab[2][$x] = ' ') Then
		$tab[2][$x] = 'O'
		$w = True
	ElseIf ($tab[0][$x] = 'O' and $tab[2][$x] = 'O' and $tab[1][$x] = ' ') Then
		$tab[1][$x] = 'O'
		$w = True
	ElseIf ($tab[1][$x] = 'O' and $tab[2][$x] = 'O' and $tab[0][$x] = ' ') Then
		$tab[0][$x] = 'O'
		$w = True
	ElseIf ($tab[0][0] = 'O' and $tab[1][1] = 'O' and $tab[2][2] = ' ') Then
		$tab[2][2] = 'O'
		$w = True
	ElseIf ($tab[0][0] = 'O' and $tab[2][2] = 'O' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$w = True
	ElseIf ($tab[1][1] = 'O' and $tab[2][2] = 'O' and $tab[0][0] = ' ') Then
		$tab[0][0] = 'O'
		$w = True
	ElseIf ($tab[2][0] = 'O' and $tab[1][1] = 'O' and $tab[0][2] = ' ') Then
		$tab[0][2] = 'O'
		$w = True
	ElseIf ($tab[2][0] = 'O' and $tab[0][2] = 'O' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$w = True
	ElseIf ($tab[1][1] = 'O' and $tab[0][2] = 'O' and $tab[2][0] = ' ') Then
		$tab[2][0] = 'O'
		$w = True
	EndIf
if $w then ExitLoop

Next
if $w = False Then

ConsoleWrite(' fail'&@CRLF)
		Return 0
	Else
Return 1
EndIf

EndFunc

func blokuj()
ConsoleWrite('próba  blokowania'&@CRLF)
$b= False
for $zz = 0 to 2
	ConsoleWrite($zz&@CRLF)
	if ($tab[$zz][0] = 'X' and $tab[$zz][1] = 'X' and $tab[$zz][2] = ' ' ) Then
		$tab[$zz][2] = 'O'
		$b=True
	ElseIf ($tab[$zz][1] = 'X' and $tab[$zz][2] = 'X' and $tab[$zz][0] = ' ') Then
		$tab[$zz][0] = 'O'
		$b=True
	ElseIf ($tab[$zz][0] = 'X' and $tab[$zz][2] = 'X' and $tab[$zz][1] = ' ') Then
		$tab[$zz][1] = 'O'
		$b=True

	ElseIf ($tab[0][$zz] = 'X' and $tab[1][$zz] = 'X' and $tab[2][$zz] = ' ') Then
		$tab[2][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][$zz] = 'X' and $tab[2][$zz] = 'X' and $tab[1][$zz] = ' ') Then
		$tab[1][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][$zz] = ' ' and $tab[1][$zz] = 'X' and $tab[2][$zz] = 'X') Then
		$tab[0][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][0] = 'X' and $tab[1][1] = 'X' and $tab[2][2] = ' ') Then
		$tab[2][2] = 'O'
		$b=True
	ElseIf ($tab[0][0] = 'X' and $tab[2][2] = 'X' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$b=True
	ElseIf ($tab[1][1] = 'X' and $tab[2][2] = 'X' and $tab[0][0] = ' ') Then
		$tab[0][0] = 'O'
		$b=True
	ElseIf ($tab[2][0] = 'X' and $tab[1][1] = 'X' and $tab[0][2] = ' ') Then
		$tab[0][2] = 'O'
		$b=True
	ElseIf ($tab[2][0] = 'X' and $tab[0][2] = 'X' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$b=True
	ElseIf ($tab[1][1] = 'X' and $tab[0][2] = 'X' and $tab[2][0] = ' ') Then
		$tab[2][0] = 'O'
		$b=True
	EndIf
if $b then ExitLoop

Next
if $b = False Then
		ConsoleWrite('blokuj fail'&@CRLF)
		Return 0
	Else
Return 1
EndIf

EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
			Exit
	EndSelect
EndFunc

 

Opublikowano

To wrzuć w pętlę:

 

 

if $przycisk = 3 Then
		if $tab[0][0] = ' ' Then
			$tab[0][0] = $znak
			$klik = True
		EndIf
	ElseIf $przycisk = 4 Then
		if $tab[1][0] = ' ' Then
			$tab[1][0] = $znak
			$klik = True
		EndIf
	ElseIf $przycisk = 5 Then
		if $tab[2][0] = ' ' Then
		$tab[2][0] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 6 Then
		if $tab[0][1] = ' ' Then
		$tab[0][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 7 Then
		if $tab[1][1] = ' ' Then
		$tab[1][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 8 Then
		if $tab[2][1] = ' ' Then
		$tab[2][1] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 9 Then
		if $tab[0][2] = ' ' Then
		$tab[0][2] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 10 Then
		if $tab[1][2] = ' ' Then
		$tab[1][2] = $znak
		$klik = True
		EndIf
	ElseIf $przycisk = 11 Then
		if $tab[2][2] = ' ' Then
		$tab[2][2] = $znak
		$klik = True
		EndIf

 

 

To też się da w pętlę, tylko poszukaj w internecie, gdzieś widziałem :D

 

 

if ($tab[$zz][0] = 'X' and $tab[$zz][1] = 'X' and $tab[$zz][2] = ' ' ) Then
		$tab[$zz][2] = 'O'
		$b=True
	ElseIf ($tab[$zz][1] = 'X' and $tab[$zz][2] = 'X' and $tab[$zz][0] = ' ') Then
		$tab[$zz][0] = 'O'
		$b=True
	ElseIf ($tab[$zz][0] = 'X' and $tab[$zz][2] = 'X' and $tab[$zz][1] = ' ') Then
		$tab[$zz][1] = 'O'
		$b=True

	ElseIf ($tab[0][$zz] = 'X' and $tab[1][$zz] = 'X' and $tab[2][$zz] = ' ') Then
		$tab[2][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][$zz] = 'X' and $tab[2][$zz] = 'X' and $tab[1][$zz] = ' ') Then
		$tab[1][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][$zz] = ' ' and $tab[1][$zz] = 'X' and $tab[2][$zz] = 'X') Then
		$tab[0][$zz] = 'O'
		$b=True
	ElseIf ($tab[0][0] = 'X' and $tab[1][1] = 'X' and $tab[2][2] = ' ') Then
		$tab[2][2] = 'O'
		$b=True
	ElseIf ($tab[0][0] = 'X' and $tab[2][2] = 'X' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$b=True
	ElseIf ($tab[1][1] = 'X' and $tab[2][2] = 'X' and $tab[0][0] = ' ') Then
		$tab[0][0] = 'O'
		$b=True
	ElseIf ($tab[2][0] = 'X' and $tab[1][1] = 'X' and $tab[0][2] = ' ') Then
		$tab[0][2] = 'O'
		$b=True
	ElseIf ($tab[2][0] = 'X' and $tab[0][2] = 'X' and $tab[1][1] = ' ') Then
		$tab[1][1] = 'O'
		$b=True
	ElseIf ($tab[1][1] = 'X' and $tab[0][2] = 'X' and $tab[2][0] = ' ') Then
		$tab[2][0] = 'O'
		$b=True
	EndIf

 

846331404756772371599.jpeg
Opublikowano

Witam.

Dzięki za odpowiedź.

Nie bardzo wiem jaką pętlę masz na myśli w pierwszym przypadku. Jedyna co przyszło mi do głowy to zmiana instrukcji warunkowej if $przycisk na Switch $przycisk co dało parenaście znaków mniej.

W 2 podpowiedzi tym bardziej nie widzę możliwości zapętlenia ;)

Co najwyzej mogę skrócić trochę kod stosując BitAnd zamiast (...and...)

Pozdro

Opublikowano
Co najwyzej mogę skrócić trochę kod stosując BitAnd zamiast (...and...)

 

zmienisz całkowicie działanie

 

Spróbuj zrobić takie kółko i krzyżyk dla planszy o rozmiarach możliwych do podania przez użytkownika to zobaczysz o co mu chodziło.

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...